package com.wenhui.schoolyardinfo.square.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnFirstSecondThirdViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnFirstSecondViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnSecondThirdViewClickListener;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.firstpage.adapter.CommentsAdapter;
import com.wenhui.schoolyardinfo.firstpage.bean.BlogBean;
import com.wenhui.schoolyardinfo.manager.DateManager;
import com.wenhui.schoolyardinfo.square.bean.InterestedPersonBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/22 16:51
 * introduce : 关注vAdapter
 */

public class ConcernAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<InterestedPersonBean> interestedPersonBeanList = new ArrayList<>();
    private List<BlogBean> blogBeanList = new ArrayList<>();
    private InterestedPersonAdapter interestedPersonAdapter;
    private CommentsAdapter commentsAdapter;
    private LinearLayoutManager layoutManager;
    private static final int HEADER_VIEW_TYPE = 1;
    private static final int CONTENT_VIEW_TYPE = 2;

    public ConcernAdapter(Context context) {
        this.context = context;
    }

    public ConcernAdapter(Context context, List<InterestedPersonBean> interestedPersonBeanList, List<BlogBean> blogBeanList) {
        this.context = context;
        this.interestedPersonBeanList = interestedPersonBeanList;
        this.blogBeanList = blogBeanList;
    }

    public void clearData() {
        interestedPersonBeanList.clear();
        blogBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<InterestedPersonBean> interestedPersonBeanList, List<BlogBean> blogBeanList) {
        this.interestedPersonBeanList.addAll(interestedPersonBeanList);
        this.blogBeanList.addAll(blogBeanList);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case HEADER_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_concern_header, parent, false);
                return new HeaderHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_blog, parent, false);
                return new ContentHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HeaderHolder) {
            HeaderHolder headerHolder = (HeaderHolder) holder;
            layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
            headerHolder.interestedRecyclerView.setLayoutManager(layoutManager);
            headerHolder.interestedRecyclerView.setItemAnimator(new DefaultItemAnimator());
            interestedPersonAdapter = new InterestedPersonAdapter(context, interestedPersonBeanList);
            headerHolder.interestedRecyclerView.setAdapter(interestedPersonAdapter);
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            //加载头像
            Glide.with(context).load(blogBeanList.get(position - 1).getHeadPortraitUrl()).into(contentHolder.imvHeadPortrait);
            contentHolder.tevName.setText(blogBeanList.get(position - 1).getName());
            contentHolder.tevTime.setText(DateManager.millisecondConvertedToDate(blogBeanList.get(position - 1).getTime()));
            contentHolder.tevPublishText.setText(blogBeanList.get(position - 1).getPublishMessage());
            //加载风景
            if (blogBeanList.get(position - 1).getSceneryUrlList() != null && blogBeanList.get(position - 1).getSceneryUrlList().size() > 0) {
                contentHolder.layoutScenery.setVisibility(View.VISIBLE);
                switch (blogBeanList.get(position - 1).getSceneryUrlList().size()) {
                    case 1:
                        contentHolder.layoutSceneryI.setVisibility(View.VISIBLE);
                        contentHolder.layoutSceneryIi.setVisibility(View.GONE);
                        contentHolder.layoutSceneryIii.setVisibility(View.GONE);
                        Glide.with(context).load(blogBeanList.get(position - 1).getSceneryUrlList().get(0)).into(contentHolder.imvSceneryIA);
                        break;
                    case 2:
                        contentHolder.layoutSceneryI.setVisibility(View.GONE);
                        contentHolder.layoutSceneryIi.setVisibility(View.VISIBLE);
                        contentHolder.layoutSceneryIii.setVisibility(View.GONE);
                        Glide.with(context).load(blogBeanList.get(position - 1).getSceneryUrlList().get(0)).into(contentHolder.imvSceneryIiA);
                        Glide.with(context).load(blogBeanList.get(position - 1).getSceneryUrlList().get(1)).into(contentHolder.imvSceneryIiB);
                        break;
                    case 3:
                        contentHolder.layoutSceneryI.setVisibility(View.GONE);
                        contentHolder.layoutSceneryIi.setVisibility(View.GONE);
                        contentHolder.layoutSceneryIii.setVisibility(View.VISIBLE);
                        Glide.with(context).load(blogBeanList.get(position - 1).getSceneryUrlList().get(0)).into(contentHolder.imvSceneryIiiA);
                        Glide.with(context).load(blogBeanList.get(position - 1).getSceneryUrlList().get(1)).into(contentHolder.imvSceneryIiiB);
                        Glide.with(context).load(blogBeanList.get(position - 1).getSceneryUrlList().get(2)).into(contentHolder.imvSceneryIiiC);
                        break;
                    default:
                        break;
                }
            } else {
                contentHolder.layoutScenery.setVisibility(View.GONE);
            }

            contentHolder.tevLabel.setText(blogBeanList.get(position - 1).getLabel());
            contentHolder.tevFireNum.setText(blogBeanList.get(position - 1).getFireNum() + "");
            contentHolder.tevFeelingNum.setText(blogBeanList.get(position - 1).getFeelingNum() + "");
            contentHolder.tevCommentNum.setText(blogBeanList.get(position - 1).getCommentNum() + "");
            contentHolder.tevSharedNum.setText(blogBeanList.get(position - 1).getSharedNum() + "");

            layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(OrientationHelper.VERTICAL);
            contentHolder.commentRecyclerView.setLayoutManager(layoutManager);
            contentHolder.commentRecyclerView.setItemAnimator(new DefaultItemAnimator());
            commentsAdapter = new CommentsAdapter(context, blogBeanList.get(position - 1).getCommentBeanList());
            contentHolder.commentRecyclerView.setAdapter(commentsAdapter);
            commentsAdapter.setRcvOnItemItemViewClickListener(new RcvOnItemItemViewClickListener() {
                @Override
                public void onItemItemClickListener(int position2, View view) {
                    rcvOnFirstSecondViewClickListener.onItemItemClickListener(position - 1, position2, view);
                }
            });
            commentsAdapter.setRcvOnSecondThirdViewClickListener(new RcvOnSecondThirdViewClickListener() {
                @Override
                public void onItemItemItemClickListener(int position2, int position3, View view) {
                    rcvOnFirstSecondThirdViewClickListener.onItemItemItemClickListener(position - 1, position2, position3, view);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_VIEW_TYPE;
        } else {
            return CONTENT_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return blogBeanList.size() + 1;
    }

    protected class HeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_interested_person)
        TextView tevInterestedPerson;
        @BindView(R.id.layout_interested_person)
        RelativeLayout layoutInterestedPerson;
        @BindView(R.id.interested_recycler_view)
        RecyclerView interestedRecyclerView;

        private HeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutInterestedPerson.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_interested_person:

                    break;
                default:
                    break;
            }
        }
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_time)
        TextView tevTime;
        @BindView(R.id.tev_come_from)
        TextView tevComeFrom;
        @BindView(R.id.imv_more)
        ImageView imvMore;
        @BindView(R.id.layout_more)
        RelativeLayout layoutMore;
        @BindView(R.id.tev_publish_text)
        TextView tevPublishText;
        @BindView(R.id.imv_scenery_i_a)
        ImageView imvSceneryIA;
        @BindView(R.id.layout_scenery_i)
        RelativeLayout layoutSceneryI;
        @BindView(R.id.imv_scenery_ii_a)
        ImageView imvSceneryIiA;
        @BindView(R.id.imv_scenery_ii_b)
        ImageView imvSceneryIiB;
        @BindView(R.id.layout_scenery_ii)
        LinearLayout layoutSceneryIi;
        @BindView(R.id.imv_scenery_iii_a)
        ImageView imvSceneryIiiA;
        @BindView(R.id.imv_scenery_iii_b)
        ImageView imvSceneryIiiB;
        @BindView(R.id.imv_scenery_iii_c)
        ImageView imvSceneryIiiC;
        @BindView(R.id.layout_scenery_iii)
        LinearLayout layoutSceneryIii;
        @BindView(R.id.layout_scenery)
        FrameLayout layoutScenery;
        @BindView(R.id.tev_label)
        TextView tevLabel;
        @BindView(R.id.layout_label)
        RelativeLayout layoutLabel;
        @BindView(R.id.imv_fire)
        ImageView imvFire;
        @BindView(R.id.tev_fire_num)
        TextView tevFireNum;
        @BindView(R.id.layout_fire)
        LinearLayout layoutFire;
        @BindView(R.id.imv_feeling)
        ImageView imvFeeling;
        @BindView(R.id.tev_feeling_num)
        TextView tevFeelingNum;
        @BindView(R.id.layout_feeling)
        LinearLayout layoutFeeling;
        @BindView(R.id.imv_comment)
        ImageView imvComment;
        @BindView(R.id.tev_comment_num)
        TextView tevCommentNum;
        @BindView(R.id.layout_comment)
        LinearLayout layoutComment;
        @BindView(R.id.imv_share)
        ImageView imvShare;
        @BindView(R.id.tev_shared_num)
        TextView tevSharedNum;
        @BindView(R.id.layout_shared)
        LinearLayout layoutShared;
        @BindView(R.id.comment_recycler_view)
        RecyclerView commentRecyclerView;
        @BindView(R.id.imv_spread)
        ImageView imvSpread;
        @BindView(R.id.layout_spread)
        RelativeLayout layoutSpread;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            imvHeadPortrait.setOnClickListener(this);
            layoutMore.setOnClickListener(this);
            layoutFire.setOnClickListener(this);
            layoutFeeling.setOnClickListener(this);
            layoutComment.setOnClickListener(this);
            layoutShared.setOnClickListener(this);
            layoutSpread.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.imv_head_portrait:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_more:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_fire:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_feeling:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_comment:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_shared:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_spread:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }

    private RcvOnFirstSecondViewClickListener rcvOnFirstSecondViewClickListener;

    public void setRcvOnFirstSecondViewClickListener(RcvOnFirstSecondViewClickListener rcvOnFirstSecondViewClickListener) {
        this.rcvOnFirstSecondViewClickListener = rcvOnFirstSecondViewClickListener;
    }

    private RcvOnFirstSecondThirdViewClickListener rcvOnFirstSecondThirdViewClickListener;

    public void setRcvOnFirstSecondThirdViewClickListener(RcvOnFirstSecondThirdViewClickListener rcvOnFirstSecondThirdViewClickListener) {
        this.rcvOnFirstSecondThirdViewClickListener = rcvOnFirstSecondThirdViewClickListener;
    }
}
